﻿using System;
using System.Web.UI.WebControls;
using AutoStopApp.Core;
using AutoStopData.Domains;
using Telerik.Web.UI;

namespace AutoStopApp.Controls
{
    public partial class UserList : AjaxUserControl
    {
        private bool DisplayAlsoBannedUsers
        {
            get
            {
                if (ViewState["DisplayAlsoBannedUsers"] == null)
                {
                    ViewState["DisplayAlsoBannedUsers"] = false;
                }
                return (bool)ViewState["DisplayAlsoBannedUsers"];
            }
            set
            {
                ViewState["DisplayAlsoBannedUsers"] = value;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Load += PageLoad;
            odsUsers.SelectMethod = "Select";
            odsUsers.Selecting += odsTrips_Selecting;
            rgrUsers.ItemCreated += rgrUsers_ItemCreated;
        }

        void PageLoad(object sender, EventArgs e)
        {
            AddAjaxSetting(rgrUsers, rgrUsers);
        }

        protected void DisplayAlsoBannedUsersButtonClick(object sender, EventArgs e)
        {
            DisplayAlsoBannedUsers = ((RadButton)sender).Checked;
            rgrUsers.Rebind();
        }

        protected void rgrUsers_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridCommandItem)
            {
                var commandItem = e.Item as GridCommandItem;

                var DisplayAlsoBannedUsersButton = (RadButton)commandItem.FindControl("DisplayAlsoBannedUsersButton");
                DisplayAlsoBannedUsersButton.Checked = DisplayAlsoBannedUsers;
            }
        }

        void odsTrips_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            e.InputParameters.Add("displayAlsoBannedUsers", DisplayAlsoBannedUsers);
        }
    }
}